package com.example.appmanager.user;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.example.appmanager.Appconfig.Appconfig;
import com.example.appmanager.Appconfig.ToastShow;
import com.example.appmanager.R;
import com.example.appmanager.entity.UserTableEntity;
import com.example.appmanager.services.AllresposeService;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class UserFindPassword extends AppCompatActivity {
    private EditText uname;//名字
    private EditText Account;//账户
    private Button button;//这个找回密码按钮
    private ImageView imageView;//back_find
    private String msg = "";
    private String name = "";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.find_user_password);
        initUi();
        setOnClickListener();

    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    private void setOnClickListener() {

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(),Login.class);
                startActivity(intent);
            }
        });
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cheakLogin();
            }
        });
    }
    private boolean cheakLogin() {
        name = uname.getText().toString().trim();
        Log.d("UserFindPassword11111",name);
        Retrofit retrofit = new Retrofit.Builder().baseUrl(Appconfig.BASE_URL)
                .client(Appconfig.CLIENT) //这是设置请求的时间等信息 连接时间  上传等时间
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        AllresposeService request = retrofit.create(AllresposeService.class);
        Call<UserTableEntity> userCall = request.getUserPwd(name);
        userCall.enqueue(new Callback<UserTableEntity>() {
            @Override
            public void onResponse(Call<UserTableEntity> call, Response<UserTableEntity> response) {
                assert response.body() != null;
                msg = response.body().getUname();
                
                if (!(msg.isEmpty()) &&  name.equals(uname.getText().toString().trim())){
                    msg = response.body().getUpassword();
                    Log.d("UserFindPassword",msg);
                    showNormalDialog(response.body().getUpassword());
                }else {

                    Toast toast = new ToastShow().getToast(getApplicationContext(),"用户名不存在！请核对在输入....",1000);
                    toast.show();
                }
            }
            @Override
            public void onFailure(Call<UserTableEntity> call, Throwable t) {
                Toast toast = new ToastShow().getToast(getApplicationContext(),"网络请求错误....",1000);
                toast.show();
                Log.d("Login", "请求错误" + t.getMessage());
            }
        });
        return false;
    }
    private void initUi() {
        uname = findViewById(R.id.find_user_Uname);
        Account = findViewById(R.id.find_user_Account);
        button = findViewById(R.id.button_other_find);
        imageView = findViewById(R.id.back_find);

    }
    private void showNormalDialog(String pwd){
        /* @setIcon 设置对话框图标
         * @setTitle 设置对话框标题
         * @setMessage 设置对话框消息提示
         * setXXX方法返回Dialog对象，因此可以链式设置属性
         */
        AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(this);
        normalDialog.setIcon(R.drawable.dialog);
        normalDialog.setTitle("修改密码提示");
        normalDialog.setMessage("你的密码是:"+pwd);
        normalDialog.setPositiveButton("好的我知道了",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do
                    }
                });
        normalDialog.setNegativeButton("返回登录",
                (dialog, which) -> {
                    //...To-do
                    Intent intent = new Intent(getApplicationContext(),Login.class);
                    startActivity(intent);//返回登录！！！
                });
        // 显示
        normalDialog.show();
    }
}
